package com.offices.subscribe.services;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.offices.subscribe.dto.message.BaseMessage;
import com.offices.subscribe.dto.message.ImageMessage;
import com.offices.subscribe.dto.message.TextMessage;
import com.offices.subscribe.dto.message.VoiceMessage;
import com.offices.subscribe.dto.message.musicmessage.MusicMessage;
import com.offices.subscribe.dto.message.newsmessage.NewsMessage;
import com.offices.subscribe.dto.message.videomessage.Video;
import com.offices.subscribe.dto.message.videomessage.VideoMessage;
import lombok.extern.slf4j.Slf4j;
import java.util.Map;

@Slf4j
public class MessageService {

    private static XmlMapper xmlMapper;
    private static Map<String, String> globalRequestMap;

    //初始化xml实例
    static {
        xmlMapper = new XmlMapper();
        xmlMapper.setDefaultUseWrapper(false);
        //字段为null，自动忽略，不再序列化
        xmlMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //XML标签名:使用骆驼命名的属性名，
        xmlMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
        //设置转换模式
        xmlMapper.enable(MapperFeature.USE_STD_BEAN_NAMING);
    }

    public static String handleMessage(Map<String, String> requestMap) throws JsonProcessingException {
//        将发来的信息参数赋值给全局参数
        globalRequestMap = requestMap;
        //声明基础消息实体类
        BaseMessage message = null;
        switch (requestMap.get("MsgType"))
        {
            case "text":
                message = dealTextMessage();
                break;
            case "image":
                message = dealImageMessage();
                break;
            case "voice":
                message = dealVoiceMessage();
                break;
            case "video":
                message = dealVideoMessage();
                break;
            case "music":
                message = dealMusicMessage();
                break;
            case "news":
                message = dealNewsMessage();
                break;
            case "event":
                message = dealEvent();
                break;
            default:
                log.error("无法匹配当前类型：" + requestMap.get("MsgType"));
                return "无法做出相应信息类型处理";
        }
        //将实体类消息转为xml字符串
        String result = xmlMapper.writeValueAsString(message);
        System.out.println(result);
        return result;
    }

    //处理回复文本消息
    private static TextMessage dealTextMessage() {
        return new TextMessage(globalRequestMap,"content222222333333");
    }

//    处理图片信息
    private static ImageMessage dealImageMessage() {
        String mediaId = "cteeFrdNNA5wnfPhg8WqT0dUryflwfbA3Q9XsGwxAeVW2cRNz330JWLy3WgJX9fW";
        return new ImageMessage(globalRequestMap, mediaId);
    }

//    处理语音信息
    private static VoiceMessage dealVoiceMessage() {
        String mediaId = "cteeFrdNNA5wnfPhg8WqT0dUryflwfbA3Q9XsGwxAeVW2cRNz330JWLy3WgJX9fW";
        return new VoiceMessage(globalRequestMap, mediaId);
    }

//    处理视频信息
    private static VideoMessage dealVideoMessage() {
        String mediaId = "cteeFrdNNA5wnfPhg8WqT0dUryflwfbA3Q9XsGwxAeVW2cRNz330JWLy3WgJX9fW";
        return new VideoMessage(globalRequestMap, new Video(mediaId,"视频title","视频描述信息"));
    }

//    处理音乐信息
    private static MusicMessage dealMusicMessage() {
//        Music music = new Music();
        return null;
    }

//    处理图文信息
    private static NewsMessage dealNewsMessage() {
        return null;
    }

//    处理事件任务
    private static BaseMessage dealEvent() {
        BaseMessage message = null;
        String EventKey;
        switch (globalRequestMap.get("Event"))
        {
            case "CLICK":
                EventKey = globalRequestMap.get("EventKey");
                message = new TextMessage(globalRequestMap,EventKey);
                System.out.println("点击了菜单栏" + EventKey);
                break;
            case "VIEW":
                System.out.println("点击了视图菜单");
                break;
        }
        return message;
    }

}
